<?php
include_once dirname(__FILE__)."/../utils/config.php";
include dirname(__FILE__)."/../utils/utils.php";

session_start();
if(!Utils::isLoggedIn()){
	die('You are not allowed to access this page.');
}

if(@$_POST ['submit']) {
	if (@getimagesize($_FILES['file']['tmp_name'])) {
		$file = $_FILES ['file'];
		$name1 = $file ['name'];
		$type = $file ['type'];
		$size = $file ['size'];
		$tmppath = $file ['tmp_name'];
		$imageDesc_hu =  $_POST['imageDesc_hu'];
		$imageDesc_en =  $_POST['imageDesc_en'];
		$teamId = isset($_POST['teamId'])?$_POST['teamId']:'-1';
		$timeparts = explode(" ",microtime());
		$currenttime = bcadd(($timeparts[0]*1000),bcmul($timeparts[1],1000));
		$imageName = Config::IMAGE_NAME_PREFIX.$currenttime."_".$name1;
		echo "name1: ".$name1.", type: ".$type.", size: ".$size.", tmppath: ".$tmppath.", teamId: ".$teamId;

		// make a note of the directory that will recieve the uploaded file
		$uploadsDirectory = $_SERVER['DOCUMENT_ROOT'].Config::IMAGE_DIR;
		echo "uploadsDirectory: ".$uploadsDirectory;

		if($name1!="") {
			include "dbmanager.php";
			$pDatabase = Database::getInstance();
			if(move_uploaded_file ($tmppath, $uploadsDirectory.$imageName)) {//image is a folder in which you will save image{
				$sql="INSERT INTO image SET imageFileName='".$name1."', imageName='".$imageName."', teamId = ".$teamId.", imageDesc_hu ='".$imageDesc_hu."', imageDesc_en = '".$imageDesc_en."'";
				$result = $pDatabase->query($sql);
				if (!$result) {
					die('Error: ' . mysql_error());
				} else {
					header("Location: ../../index.php?site=admin&submenu=editimage&id=".$teamId);
				}
			}

		}
	} else {
		die('Error: only image uploads are allowed');
	}

} else {
	echo "nem is submit";
}
?>